import { CardColor, CurrentPlayer, ListenEventType, PanelType, playerPeople } from "../util/defineConfig";
import View from "../util/View";
import { Card } from "./gameModel";
import gamePanel from "./gamePanel/gamePanel";
import PopupPanel from "./gamePanel/popupPanel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameView extends View {


    public UICards:cc.Node[] = []
    private gamePanel:gamePanel = null
    private PopupPanel:PopupPanel = null

    public loadPanel(panel:string){
        this.node.removeAllChildren()
        this.show_ui_at(this.node,panel,this)
    }

    public loadGameScene(index:playerPeople){
        this.node.removeAllChildren()
       this.show_ui_at(this.node,PanelType.GAMEPANEL,this,false,ListenEventType.GET_GAMEPANEL_BY_VIEW,index)
    }

    public initCards(cards:Card[],backGroundCard:cc.SpriteFrame){
        cc.resources.load('/prefab/panel/card',cc.Prefab,(err,prefab)=>{
        if(err){
            console.warn(err)
        }else{
             cards.forEach((card)=>{
            let node = cc.instantiate(prefab as cc.Prefab)
            node.getComponent(cc.Sprite).spriteFrame = backGroundCard
            let UICard = node.addComponent(PanelType.UICARD)
            UICard.init(card)
            this.UICards.push(node)
        })
        }
    })   
    }

    public getGamePanel(node:cc.Node){
        this.gamePanel = node.getComponent(gamePanel)
    }

    public getPopupPanel(node:cc.Node){
        this.PopupPanel = node.getComponent(PopupPanel)
    }

    public dealCards(card:Card,player:CurrentPlayer,index:number, insertIndex:number = 0){
        this.gamePanel.dealCards(card,player,index,insertIndex)
    }

    public openSendAreaToRecycleArea(card:Card){
        this.gamePanel.openSendAreaToRecycleArea(card)
    }

    public recycleCard(card:Card,play:CurrentPlayer){
        this.gamePanel.recycleCard(card,play)
    }

    public selectCard(card:Card,play:CurrentPlayer){
        this.gamePanel.selectCard(card,play)
    }

    public changeUIColor(color:CardColor){
        this.gamePanel.setBtnColorState(color)
    }

    public robotDealCard(card:Card,player:CurrentPlayer){
        this.gamePanel.robotDealCard(card,player)
    }

    public dealAreaToPlayArea(card:Card,player:CurrentPlayer,index:number){
        this.gamePanel.dealCardByPlay(card,player,index)
    }

    public changeUIBar(player:CurrentPlayer){
        this.gamePanel.setBarState(player)
    }

    public showSelectColorPanel(){
        this.gamePanel.showSelectColorPanel()
    }

    public postUNO(player:CurrentPlayer){
        this.gamePanel.setUNO(player)
    }

    public postPoints(player:CurrentPlayer, points:number){
        this.gamePanel.setPoints(player, points)
    }

    public postSuitableCards(cards:Card[]){
        this.gamePanel.showSuitableCards(cards)
    }

    public showGameoverPanel(isWin:boolean){
        this.gamePanel.showGameoverPanel(isWin)
    }

    public getCardByType(arr:cc.SpriteFrame[]){
       this.PopupPanel.getCard(arr)
    }

    public isGameOver(isGameOver:boolean){
        this.gamePanel.handleIsGameOver(isGameOver)
    }

    }
